'''
Created on 2010/07/06

@author: yukiyonezawa
'''

from pyrfid.DataProtocol.AddObject import AddObject
from pyrfid.asn1.Encoder import Encoder
from pyrfid.asn1.Decoder import Decoder


class ReadObjectsCRU(object):
    '''
    classdocs
    '''
    dsfid = object()
    dsfid_on_tag = object()
    access_method = object()
    command_list = object()
    TAG_ID = str()
    
    READ_1st_OBJECTS = 0
    READ_MULTIPLE_OBJECTS = 1    
    READ_ALL_OBJECTS = 2    
    driver = object
        
    COMPLETION_CODE = 0
    EXECUTION_CODE = 0
    
    def __init__(self, tag_id, dsfid, command_list, driver):
        '''
        Constructor
        '''
        self.TAG_ID = tag_id
        self.dsfid_on_tag = dsfid
        self.command_list = command_list
        self.driver = driver
        
    def execute(self):
        
        #read_type
        read_type = self.command_list.popElement().VALUE
        
        #30
        self.command_list.popElement().VALUE
        for index in range(self.command_list.getLengthElement() / 2):
            object_identifier = self.command_list.popElement().VALUE
            is_duplicate = self.command_list.popElement().VALUE
            print object_identifier, is_duplicate
        
        print self.dsfid_on_tag
        self.dsfid_on_tag.access_method.setDriver(self.driver)
        self.added_object_list = self.dsfid_on_tag.access_method.readData(self.TAG_ID, self.dsfid_on_tag.data_format, read_type)
    
    def encode(self, cru_id):
        encoder = Encoder()
        listRSPAndData = []
        listRSPAndData.append(['06', '05', Encoder.oidEncode(cru_id)])
        listRSPAndData.append([Decoder.SEQUENCE_AND_SEQUENCE_OF, '', ''])
        
        #completion code:INTEGER
        listRSPAndData.append([Decoder.INTEGER, '01', encoder.intEncode(self.COMPLETION_CODE) ])
        
        #execution code:INTEGER
        listRSPAndData.append([Decoder.INTEGER, '01', encoder.intEncode(self.EXECUTION_CODE)])
        
        #objects:SEQUENCE OF SEQUENCE
        listRSPAndData.append([Decoder.SEQUENCE_AND_SEQUENCE_OF, '', ''])
        
        objectLength = 0
#        if self.EXECUTION_CODE == self.NO_ERROR and self.COMPLETION_CODE == self.NO_ERROR:
        for add_object in self.added_object_list:
            
            #objectId:OBJECT_IDENTIFIER
            objectIdentifierLengthHex = encoder.lengthEncode(len(add_object.OBJECT_IDENTIFIER) / 2)
            listRSPAndData.append([Decoder.OBJECT_IDENTIFIER, objectIdentifierLengthHex, add_object.OBJECT_IDENTIFIER])
            objectLength += int(objectIdentifierLengthHex, 16)
        
            #object:OCTET_STRING
            objectLengthHex = encoder.lengthEncode(len(add_object.OBJECT) / 2 )
            listRSPAndData.append([Decoder.OCTET_STRING, objectLengthHex , add_object.OBJECT])
            objectLength += int(objectLengthHex, 16)
        
            #compactParameter:INTEGER
            listRSPAndData.append([Decoder.INTEGER, '01', encoder.intEncode(int(add_object.COMPACT_PARAMETER))])
            objectLength += 1
        
            #lockStatus:BOOLAN
            listRSPAndData.append([Decoder.BOOLEAN, '01', encoder.boolEncode('False')])
            objectLength += 1
        
        listRSPAndData[4][1] = encoder.lengthEncode(objectLength)
        allLength = objectLength + 1 + 1
        listRSPAndData[1][1] = encoder.lengthEncode(allLength)
        
        #print 'ListRSP: '
        # for l in self.listRSPAndData:
        #             print l
        #        
        rspHex = str()
        for l in listRSPAndData:
            for s in l:
                rspHex += s
        return rspHex


